# koroFileHeader at Home PC
# Create: 2021-04-10 14:00:59
# LastEdit: 2021-04-11 11:03:05
"""继承类，然后替换掉部分代码，以实现功能"""
__author__ = '749B'

from azure.mgmt.resourcegraph import ResourceGraphClient

from typing import Optional, Dict, Any

class MyResourceGraphClient(ResourceGraphClient):
    """
    源码中把api-version写死了，但中国区又不支持最新的api-version。
    替换掉提交post请求的方法，先检查提交的api-version的值，然后替换掉。
    再调用原来的post请求并返回。
    """

    def __init__(self, *args, **kwargs):
        super(MyResourceGraphClient, self).__init__(*args, **kwargs)
        self.old_post = self._client.post
        self._client.post = self.my_post

    def my_post(self,
        url,  # type: str
        params=None,  # type: Optional[Dict[str, str]]
        headers=None,  # type: Optional[Dict[str, str]]
        content=None,  # type: Any
        form_content=None,  # type: Optional[Dict[str, Any]]
        stream_content=None,  # type: Any
    ):
        # type: (...) -> HttpRequest
        if params.get('api-version') == '2020-04-01-preview':
            params['api-version'] = '2019-04-01'
        return self.old_post(url, params, headers, content, form_content, stream_content)
